package com.longrise.co.ui.home.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.longrise.co.R;
import com.longrise.co.ui.home.contract.HomeContract;
import com.longrise.co.ui.home.model.CompanyDetail;
import com.longrise.co.ui.home.model.HomeModel;
import com.longrise.co.ui.home.presenter.HomePresenter;
import com.ly.common.base.BaseFragment;
import com.ly.common.commonutils.ImageLoaderUtils;
import com.ly.common.commonutils.StringUtils;
import com.ly.common.dialog.DialogOnClickListener;
import com.ly.common.dialog.MDAlertDialog;

import butterknife.Bind;
import butterknife.OnClick;
import longrise.phone.com.baidulib.baidu.MapNavUtils;

/**
 * Created by luoyang on 2017-7-21.
 */

public class CompanyDetailFragment extends BaseFragment<HomePresenter, HomeModel> implements HomeContract.View<CompanyDetail>,DialogOnClickListener {

    @Bind(R.id.company_photo)
    ImageView companyPhoto;
    @Bind(R.id.ratingBar)
    RatingBar ratingBar;
    @Bind(R.id.company_name)
    TextView companyName;
    @Bind(R.id.state)
    TextView state;
    @Bind(R.id.address_tv)
    TextView addressTv;
    @Bind(R.id.call_tv)
    TextView callTv;
    @Bind(R.id.levels)
    TextView levels;
    @Bind(R.id.major_brands)
    TextView majorBrands;
    @Bind(R.id.warranty_msg)
    TextView warrantyMsg;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.time)
    TextView time;
    @Bind(R.id.count)
    TextView count;
    @Bind(R.id.flower_three_value)
    TextView flowerThreeValue;
    @Bind(R.id.flower_two_value)
    TextView flowerTwoValue;
    @Bind(R.id.flower_one_value)
    TextView flowerOneValue;

    private String mCompanyId;
    private String mAreaId;

    private MDAlertDialog mDialog;

    private CompanyDetail mData ;

    private MapNavUtils mapNavUtils;


    @Override
    protected int getLayoutResource() {
        return R.layout.fg_company_detail;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }


    @Override
    protected void initView() {
        mCompanyId = getArguments().getString("companyId");
        mAreaId = getArguments().getString("areaId");
        mapNavUtils = new MapNavUtils(getActivity());
        mPresenter.getCompanyDetail(mCompanyId, mAreaId);
    }


    @Override
    public void requestSuccess(CompanyDetail bean) {
        if(bean == null) {
            return;
        }
        try {
            mData = bean;
            if (!StringUtils.isEmpty(bean.getCompanyimg())) {
                ImageLoaderUtils.display(getContext(), companyPhoto, bean.getCompanyimg());
            }
            if(!StringUtils.isEmpty(bean.getStar())) {
                ratingBar.setRating(Float.parseFloat(bean.getStar()));
            }

            setText(companyName, bean.getCompanyname());
            if("1".equals(bean.getState())) {
                //已完成对接
                state.setText("已完成与全国汽车电子健康档案系统数据对接");
            } else {
                state.setText("未完成与全国汽车电子健康档案系统数据对接");
            }

            setText(addressTv, bean.getCompanyaddress());

            setText(callTv, bean.getLinktel());
            setText(levels, bean.getLevels());
            setText(majorBrands, bean.getMajorbrands());
            setText(warrantyMsg, bean.getWarrantymsg());
            setText(title, bean.getRecentactivities());
            setText(flowerThreeValue, bean.getRatenum());
            setText(flowerTwoValue, bean.getMiddlenum());
            setText(flowerOneValue, bean.getBadnum());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initDialog() {
        mDialog = new MDAlertDialog.Builder(getContext())
                .setContentText("确定拨打该企业电话？")
                .setOnclickListener(this)
                .build();
    }


    public static CompanyDetailFragment newInstance(String companyId, String areaId) {

        Bundle args = new Bundle();
        args.putString("companyId", companyId);
        args.putString("areaId", areaId);
        CompanyDetailFragment fragment = new CompanyDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onRequestComplete(int type) {

    }

    @OnClick({R.id.head_back,R.id.complaint_call_iv, R.id.complaint_location_iv})
    public void onViewClicked(View view) {
        if(checkDoubleClick()) {
            switch (view.getId()) {
                case R.id.head_back :
                    _mActivity.onBackPressed();
                    break;
                case R.id.complaint_call_iv :
                    if(mDialog == null) {
                        initDialog();
                    }
                    mDialog.show();
                    break;
                case R.id.complaint_location_iv :
                    if(mData == null || StringUtils.isEmpty(mData.getLongitude()) || StringUtils.isEmpty(mData.getLatitude())) {
                        showLongToast("地理信息位置不全，无法为您导航");
                        return;
                    }

                    double lat = Double.parseDouble(mData.getLatitude());
                    double lon = Double.parseDouble(mData.getLongitude());
                    mapNavUtils.start(lat, lon,null);
                    break;
            }
        }

    }

    @Override
    public void clickLeftButton(View view) {
        if(mDialog != null) {
            mDialog.dismiss();
        }
    }

    @Override
    public void clickRightButton(View view) {

        //拨打电话
        if(mData != null && !StringUtils.isEmpty(mData.getLinktel())) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + mData.getLinktel()));
            startActivity(intent);
        }

        if(mDialog != null) {
            mDialog.dismiss();
        }
    }
}
